package com.hzsai.interceptors;

import org.springframework.lang.Nullable;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * hzsai 2019/7/31
 * usage:
 **/
public class MyInterceptor implements HandlerInterceptor {

    /**
     * 拦截器的执行流程：
     * 1. 执行preHandle方法             ----> 返回true或false，如果返回false，直接到步骤6
     * 2. 执行controller里面方法         ----> 顺序执行
     * 3. 执行postHandle方法             ----> 顺序执行
     * 4. 执行视图渲染(如果有）           ----> 顺序执行
     * 5. 执行afterCompletion方法        ----> 顺序执行
     * 6. 后续步骤                       ----> 顺序执行
     */

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // before controller
        System.out.println("控制器前");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable ModelAndView modelAndView) throws Exception {
        System.out.println("控制器post");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        System.out.println("控制器完成");
    }
}
